<?php
/**
 * 《电话费率》实现类
 *
 * @author 孙晓晔
 * @version $Id: Rate.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

class Rate extends TelUtil {
	public $tbl = Constant::tbl_rate;

	public $fixed = array(
		'-' => '请选择',
		'1' => '固话',
		'2' => '手机',
	);

	public $orderby = array(
		'-' => '请选择',
		'zone' => '区号',
		'rate' => '费率',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);

		$controller->assign_by_ref('fixed_option', $this->fixed);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'tel/rate/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {

		$name_s = $request['name_s'];

		$zone_s = $request['zone_s'];
		$continent_s = $request['continent_s'];
		$fixed_s = $request['fixed_s'];

		$where = " WHERE 1=1";

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR country LIKE '%" . $name_s . "%')";
		}

		if (!empty($zone_s)) {
			$where .= " AND zone = '" . $zone_s . "'";
		}

		if (!empty($continent_s) && $continent_s <> '-') {
			$where .= " AND continent = '" . $continent_s . "'";
		}

		if (!empty($fixed_s) && $fixed_s <> '-') {
			$where .= " AND fixed = '" . $fixed_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row['fixed'] = $this->fixed[$row['fixed']];
				$row['continent'] = $this->continent[$row['continent']];

				$row['status_t'] = $this->status[$row['status']];

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/rate/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'tel/rate/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$zone = $request['zone'];
		$rate = $request['rate'];

		$name = $request['name'];
		$country = addslashes($request['country']);

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(zone, rate, name, country, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $zone, $rate, $name, $country, $status);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/rate/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$zone = $request['zone'];
		$rate = $request['rate'];

		$name = $request['name'];
		$country = addslashes($request['country']);

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET zone = '%s', rate = '%s', name = '%s', country = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $zone, $rate, $name, $country, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'tel/rate/edit');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		} 
		
		$row['rule_t'] = $this->rule[$row['rule']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function rate($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_r);
		$fmt = "select zone, rate from %s order by zone";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$dept = '0';
		while ($row = $db->fetch_array($result)) {
			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$request['tpl'] = 'txt';

		$controller->display($request, 'country/rate');
		return true;
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function a($controller, $request) {
		$db = newdb($this->dsn);
		$fd = fopen("/home/ftp/tecmold.com/ns/tecmold/doc/country.txt", "r");
		while (!feof($fd)) {
			$s = trim(fgets($fd));
			if (strlen($s) < 5) {
				continue;
			}

			$a = preg_split("/[\\s,]+/", trim($s));
			// print_r($a);
			$eng = $a[0];
			$name = $a[1];

			$country = '';
			for ($i = 2; $i < count($a); $i++) {
				$country .= " " . $a[$i];
			}

			$fmt = "insert into admin.country(eng, name, country, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $eng, $name, $country);
			// debug($sql);
			$db->query($sql);
		}

		$controller->display($request, 't');
		return true;
	}
	
	function c($controller, $request) {

		include(APP_LIB . '/simple_html_dom.php');
		$url = 'http://www.vbuzzer.com/pc_to_phone_rates.php?s_carrier_id_name=%s';

		$db = newdb($this->dsn);
		$sql = "SELECT * FROM admin.rate WHERE id > 443 order by id";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$html = file_get_html(sprintf($url, $row['zone']));
			// $html = file_get_html(sprintf($url, "3069"));

			$a = 0;
			$b = array();

			// find all td tags with attribite align=center
			foreach($html->find('font[color=black]') as $e) {
				$a = $e->innertext;

				if (preg_match("/^\\$/i", $a)) {
					$b[] = ltrim($a, "$");
				}
				// echo $e->innertext . "\n\r";
			}
			if (count($b) == 0) {
				continue;
			}

			rsort($b, SORT_NUMERIC);
			// print_r($b);
			$fmt = "UPDATE %s SET rate = '%s', modifytime = now() WHERE id = '%s'";
			$sql = sprintf($fmt, Constant::tbl_rate, $b[0], $row['id']);
			// debug($sql);
			$db->query($sql);

			// print_r($row);
			echo "\n\r";

			$html->clear();
		}

		logdate($controller, $request);
		return true;
	}
}
?>